package com.sttk.fastMark.camera.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.sttk.fastMark.camera.BaseAc;
import com.sttk.fastMark.camera.R;
import com.sttk.fastMark.camera.bean.WatermarkBean;
import com.sttk.fastMark.camera.databinding.ActivityPhotographerBinding;

public class PhotographerActivity extends BaseAc<ActivityPhotographerBinding> implements View.OnClickListener {

    private WatermarkBean.PhotographerBean photographerBean;

    @Override
    protected int onCreate() {
        return R.layout.activity_photographer;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPhotographerBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.ivTitleClear.setOnClickListener(this);
        mDataBinding.ivContentClear.setOnClickListener(this);
        mDataBinding.tvSave.setOnClickListener(this);

        mDataBinding.etTitle.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String str = charSequence.toString();
                if (TextUtils.isEmpty(str)) {
                    mDataBinding.ivTitleClear.setVisibility(View.GONE);
                } else {
                    mDataBinding.ivTitleClear.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        mDataBinding.etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String str = charSequence.toString();
                if (TextUtils.isEmpty(str)) {
                    mDataBinding.ivContentClear.setVisibility(View.GONE);
                } else {
                    mDataBinding.ivContentClear.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.ivTitleClear) {//标题清空
            mDataBinding.etTitle.setText("");
        } else if (id == R.id.ivContentClear) {//内容清空
            mDataBinding.etContent.setText("");
        } else if (id == R.id.tvSave) {//保存
            String title = mDataBinding.etTitle.getText().toString();
            String content = mDataBinding.etContent.getText().toString();
            if (TextUtils.isEmpty(title)) {
                ToastUtils.showShort(getString(R.string.please_input_title));
                return;
            }
            if (TextUtils.isEmpty(content)) {
                ToastUtils.showShort(getString(R.string.please_input_content));
                return;
            }
            photographerBean = new WatermarkBean.PhotographerBean(title, content);
            EditCustomWatermarkActivity.watermarkBean.getCustomBean().setOpenPhotographer(true);
            EditCustomWatermarkActivity.watermarkBean.getCustomBean().setPhotographerBean(photographerBean);
            finish();
        }
    }

    @Override
    protected void initData() {
        photographerBean = EditCustomWatermarkActivity.watermarkBean.getCustomBean().getPhotographerBean();
        if (photographerBean != null) {
            mDataBinding.etTitle.setText(photographerBean.getTitle());
            mDataBinding.etContent.setText(photographerBean.getTitle());
        }
    }
}